//
//  MLAManageBrandVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/20.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAManageBrandVC.h"
#import "MLAManageBrandCell.h"
#import "MLAManageBrandModel.h"

@implementation MLAManageBrandVC {
    NSMutableArray<MLAManageBrandModel *> *brandsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configTitle:@"品牌"];
    
    [self.tableView registerClass:[MLAManageBrandCell class] forCellReuseIdentifier:kManageBrandID];
    
    self.haveRefreshHeader = YES;
    [self loadData];
}

#pragma mark - LoadData
- (void)loadData {
    [super loadData];
    
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kBrandList params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self->brandsArr = [NSMutableArray array];
        self.page++;
        self.haveRefreshFooter = YES;
        self->brandsArr = [NSMutableArray array];
        self->brandsArr = [MLAManageBrandModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        if (self->brandsArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        [self.tableView reloadData];

    } failure:^{
        
    }];
    
}

- (void)loadMoreData {
    NSDictionary *params = @{@"page":@(self.page)};
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kBrandList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        
        self.page++;
        
        if ([response[@"lists"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        
        [self->brandsArr addObjectsFromArray:[MLAManageBrandModel mj_objectArrayWithKeyValuesArray:response[@"lists"]]];
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
}


#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return brandsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MLAManageBrandCell *cell = [tableView dequeueReusableCellWithIdentifier:kManageBrandID forIndexPath:indexPath];
    
    MLAManageBrandModel *model = brandsArr[indexPath.row];
    cell.model = model;
    
    return cell;
}



@end
